package com.liu.nb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.nb.entity.Role;
import com.liu.nb.entity.RolePage;
import com.liu.nb.entity.SysUser;
import com.liu.nb.mapper.RoleMapper;
import com.liu.nb.mapper.RolePageMapper;
import com.liu.nb.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.nb.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author zk
 * @since 2021-09-02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    //获取角色信息
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private IUserService iUserService;
    @Resource
    private PageServiceImpl pageService;
    @Override
    public IPage<Role> getRole(Integer pageCurrent,Integer pageSize,String name, Integer state) {
        Page<Role> page = new Page<>(pageCurrent,pageSize);//设置分页参数
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotNull(name),"name", name)
                .eq(ObjectUtil.isNotNull(state),"state",state);
        IPage<Role> rolePage = roleMapper.selectPage(page,queryWrapper);
        List<Role> records = rolePage.getRecords();
        records.forEach(i->{
            SysUser user = iUserService.getOne(new QueryWrapper<SysUser>().eq("id", i.getCreateUserId()));
            i.setPermsId(pageService.getPermIdsByRole(i.getId()));
            i.setUser(user);
        });
        return rolePage;
    }
}
